<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_dodetail
 * 
 * Table	: dodetail
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_dodetail extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	
	function get_barang_from_poclientdet($po,$start, $page, $limit)
	{
		/* $query  = $this->db->query("SELECT pd.NOMOR_PO as NOMOR_PO,mb.KODE_BARANG AS KODE_BARANG,pd.NAMA_BARANG AS NAMA_BARANG,pd.JUMLAH AS JUMLAH,
		pd.SATUAN AS SATUAN,pd.HARGA_SATUAN AS HARGA_SATUAN,pd.TARGET_KIRIM_BRG AS TARGET_KIRIM_BRG,mb.JENIS_BARANG AS JENIS_BARANG,
		mb.HSCODE AS HSCODE,mb.STOCK AS STOCK
		FROM poclientdet pd
		INNER JOIN mstbarang mb ON mb.NAMA_BARANG=pd.NAMA_BARANG
		WHERE pd.NOMOR_PO='$po'")->result(); */
		
		$this->db->select("NOMOR_PO,KODE_BARANG,poclientdet.NAMA_BARANG,JUMLAH,poclientdet.SATUAN,HARGA_SATUAN,TARGET_KIRIM_BRG,JENIS_BARANG,HSCODE,STOCK");
		$query = $this->db->where('NOMOR_PO',$po)->from('poclientdet')->join('mstbarang','mstbarang.NAMA_BARANG = poclientdet.NAMA_BARANG','inner')->limit($limit, $start)->order_by('poclientdet.NAMA_BARANG', 'ASC')->get()->result();
		
		//$total  = $this->db->get_where('poclientdet',array('NOMOR_PO',$po))->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
			'success'   => TRUE,
			'message'   => "Loaded data",
			//'total'     => $total,
			'data'      => $data
		);
		
		return $json;
	}
	
	function getSisa($po,$kd_barang,$nm_barang){
		$sisabarang = 0;
		$sisabarangpoc = 0;
		
		$query1 = $this->db->get_where('mstbarang',array('KODE_BARANG'=>$kd_barang))->row();
		if(sizeof($query1) > 0){
			$sisabarang = $query1->STOCK;
		}
		
		$query2 = $this->db->get_where('poclientdet',array('NOMOR_PO'=>$po,'NAMA_BARANG'=>$nm_barang))->row();
		if(sizeof($query2) > 0){
			$sisabarangpoc = $query2->JUMLAH;
		}
		
		$json	= array(
			'success'   => TRUE,
			'message'   => 'Loaded data',
			'sisabarang' 	=> $sisabarang,
			'sisabarangpoc' => $sisabarangpoc
		);
		
		return $json;
	}
	
	function getAll($no_do,$start, $page, $limit){
		$query  = $this->db->limit($limit, $start)->order_by('ID', 'DESC')->get_where('dodetail',array('NOMOR_DO'=>$no_do))->result();
		$total  = $this->db->get_where('dodetail',array('NOMOR_DO'=>$no_do))->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
			'success'   => TRUE,
			'message'   => "Loaded data",
			'total'     => $total,
			'data'      => $data
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		$pkey = array('BATCH_NO'=>$data->BATCH_NO,'NOMOR_DO'=>$data->NOMOR_DO,'KODE_BARANG'=>$data->KODE_BARANG,'ID'=>$data->ID);
		
		if($this->db->get_where('dodetail', $pkey)->num_rows() > 0){
			/*
			 * Data Exist
			 */			 
			// $tmp = substr($data->HARGA_SATUAN,3,strlen($data->HARGA_SATUAN));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->HARGA_SATUAN = $tmp;$tmp = substr($data->HARGA_TOTAL,3,strlen($data->HARGA_TOTAL));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->HARGA_TOTAL = $tmp;	
			 
			$arrdatau = array('TGL_DETAIL_DO'=>(strlen(trim($data->TGL_DETAIL_DO)) > 0 ? date('Y-m-d', strtotime($data->TGL_DETAIL_DO)) : NULL),'NOMOR_PO'=>$data->NOMOR_PO,'NAMA_BARANG'=>$data->NAMA_BARANG,'BATCH_NO'=>$data->BATCH_NO,'QUANTITY'=>$data->QUANTITY,'SATUAN'=>$data->SATUAN,'HARGA_SATUAN'=>$data->HARGA_SATUAN,'HARGA_TOTAL'=>$data->HARGA_TOTAL,'NOTE'=>$data->NOTE);
			 
			$this->db->where($pkey)->update('dodetail', $arrdatau);
			$last   = $data;
			
		}
		else
		{
			/*
			 * Data Not Exist
			 * 
			 * Process Insert
			 */
			 // $tmp = substr($data->HARGA_SATUAN,3,strlen($data->HARGA_SATUAN));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->HARGA_SATUAN = $tmp;$tmp = substr($data->HARGA_TOTAL,3,strlen($data->HARGA_TOTAL));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->HARGA_TOTAL = $tmp;
			$arrdatac = array('NOMOR_DO'=>$data->NOMOR_DO,'TGL_DETAIL_DO'=>(strlen(trim($data->TGL_DETAIL_DO)) > 0 ? date('Y-m-d', strtotime($data->TGL_DETAIL_DO)) : NULL),'NOMOR_PO'=>$data->NOMOR_PO,'KODE_BARANG'=>$data->KODE_BARANG,'NAMA_BARANG'=>$data->NAMA_BARANG,'BATCH_NO'=>$data->BATCH_NO,'QUANTITY'=>$data->QUANTITY,'SATUAN'=>$data->SATUAN,'HARGA_SATUAN'=>$data->HARGA_SATUAN,'HARGA_TOTAL'=>$data->HARGA_TOTAL,'NOTE'=>$data->NOTE);
			 
			$this->db->insert('dodetail', $arrdatac);
			$last   = $this->db->where($pkey)->get('dodetail')->row();
			
			$this->db->where(array('KODE_BARANG'=>$data->KODE_BARANG,'NAMA_BARANG'=>$data->NAMA_BARANG))->update('mstbarang', array('STOCK'=>($data->SISABARANG - $data->QUANTITY)));
			$this->db->where(array('NOMOR_PO'=>$data->NOMOR_PO,'NAMA_BARANG'=>$data->NAMA_BARANG))->update('poclientdet', array('JUMLAH'=>($data->SISAPOC - $data->QUANTITY)));
		}
		
		$total  = $this->db->get('dodetail')->num_rows();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil disimpan',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$pkey = array('NOMOR_DO'=>$data->NOMOR_DO,'KODE_BARANG'=>$data->KODE_BARANG,'ID'=>$data->ID);
		
		$this->db->where($pkey)->delete('dodetail');
		
		$total  = $this->db->get('dodetail')->num_rows();
		$last = $this->db->get('dodetail')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);				
		return $json;
	}
}
?>